package org.ovhoo.zonfig;

import java.util.ArrayList;
import java.util.Iterator;

import org.ovhoo.io.ZBufferedWriter;

/**
 * Represent a zconfig category element
 * @author mzalim
 *
 */
public class Category {
	//attributes
	public static final String defaultTitle = "genral";
	private String title;
	
	//subelements
	private ArrayList<Frame> frames;
	
	/**
	 * Constructor using the default title value
	 */
	public Category(){
		this.title = defaultTitle;
		this.frames = new ArrayList<Frame>();
	}
	
	/**
	 * constructor with a specified title
	 * @param title : The  title
	 */
	public Category(String title){
		this.title = title;
		this.frames = new ArrayList<Frame>();
	}
	
	/**
	 * returns the title
	 * @return The title
	 */
	public String getTitle(){
		return this.title;
	}
	
	/**
	 * Change the value of the attribute title
	 * @param title : the value of the attribute title
	 */
	public void setTitle(String title){
		this.title = title;
	}
	
	/***
	 * Add a frame the this category
	 * @param frame
	 */
	public void addFrame(Frame frame){
		this.frames.add(frame);
	}
	
	/**
	 * return the frame at the givin position
	 * @param position : the position of the frame to return
	 * @return  the frame found at given position
	 */
	public Frame getFrame(int position){
		return this.frames.get(position);
	}
	
	/**
	 * write the configuration file
	 * @param bufferWriter : buffer writer
	 * @param prefix : the prefix
	 */
	public void writeContent(ZBufferedWriter bufferWriter, String prefix) {
		bufferWriter.writeln(prefix + "<category title=\""+ this.title +"\">");
		
		if (this.frames != null){
			Iterator<Frame> _iterator = this.frames.iterator();
			Frame _frame;
			while(_iterator.hasNext()){
				_frame = _iterator.next();
				_frame.writeContent(bufferWriter, "	"+ prefix);
				
			}
		}
		
		bufferWriter.writeln(prefix + "</category>");
	}
	
	
}
